package com.hyc.myvideo.ui.activity.my.fragment.course;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.my.course.CourseCurrentAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.ModelCourse;
import com.hyc.myvideo.ui.activity.my.CourseDetailsActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 当前课程
 * @author gyb
 * @version 1.0
 * @created 2017/07/20
 */
public class CurrentCourseFragment extends BaseFragment implements AdapterView.OnItemClickListener{
    @BindView(R.id.common_list_id)
    PullToRefreshListView toRefreshListView;
    private CourseCurrentAdapter adapter;
    /**页数*/
    private int page = 1;
    /**是否刷新*/
    private boolean isRefresh = true;
    /**课程数据集合*/
    private List<ModelCourse> courseList = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_common_listview;
    }

    @Override
    protected void initView() {
        toRefreshListView.setOnItemClickListener(this);
        /**设置间距*/
        ListView listView= toRefreshListView.getRefreshableView();
        listView.setDivider(new ColorDrawable(res.getColor(R.color.color_0)));
        listView.setDividerHeight(15);
        courseList = new ArrayList<>();
        adapter = new CourseCurrentAdapter(getActivity(),courseList);
        toRefreshListView.setAdapter(adapter);
        toRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(true);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                requestData(false);
            }
        });
    }

    @Override
    protected void initData() {
        requestData(true);
    }

    private void requestData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            page = 1;
        }else{
            page++;
        }
        api.requestDirectData(page,0);
    }


    @Override protected void onNexts(String result, String method) {
        BaseResult<List<ModelCourse>> data = JSONObject.parseObject(result, new TypeReference<BaseResult<List<ModelCourse>>>(){});
        if(data.isCode()){
            if(isRefresh){
                courseList.clear();
            }
            courseList.addAll(data.getResult());
            adapter.updateListView(courseList);
        }
        clearedListView();

    }

    @Override protected void onError() {
        clearedListView();
    }

    /***
     * 关闭刷新
     */
    private void clearedListView(){
        if(toRefreshListView.isRefreshing()){
            toRefreshListView.onRefreshComplete();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int size = toRefreshListView.getRefreshableView().getHeaderViewsCount();
        if (courseList != null && !courseList.isEmpty()) {
            ModelCourse modelCourse = courseList.get(position - size);
            if (modelCourse != null) {
                String b_id = modelCourse.getDirect_id();
                if (!StringUtils.isEmpty(b_id)) {
                    startActivity(new Intent(getActivity(), CourseDetailsActivity.class).putExtra("b_id",b_id).putExtra("type",0));
                }
            }
        }

    }
}
